/*
 * @Author: xinyang3
 * @Date: 2021-10-08 15:51
 * @Descripttion: collection user
 * @LastEditors: xinyang3
 * @LastEditTime: 2021-11-10 15:19
 */
import { Binary } from 'mongodb';
import getMongoConnect from './client.js';
import format from '../util/date-fmt.js';
import { ObjectID } from 'mongodb';

// const dbName = 'blogs';
const collectionName = 'user';

/**
 * @description: 获取连接
 */
// async function connectCollect() {
//   const connect = await client.connect();
//   const collection = await client.db(dbName).collection(collectionName);
//   return { connect, collection };
// }

/**
 * @description: 为对象添加属性
 * @param {*} obj
 * @param {*} key
 * @param {*} value
 * @return {*}
 */
function objectSetKey(obj, key, value) {
  obj && key ? (obj[key] = value || '') : null;
}

async function query(data) {
  const { db } = await getMongoConnect();

  const { name, type, state } = data;
  const filters = [{}];
  if (name) {
    filters.push({ name: { $regex: new RegExp(`${name}`) } });
  }
  if (type) {
    filters.push({ type: { $eq: type } });
  }
  if (state) {
    filters.push({ state: { $eq: state } });
  }

  const result = await db
    .collection(collectionName)
    .find({
      $and: filters
    })
    .toArray();
  return result;
}

async function queryOne(data) {
  const { db } = await getMongoConnect();
  const { _id } = data;
  const result = await db
    .collection(collectionName)
    .findOne({ _id: { $eq: ObjectID(_id) } });
  return result;
}

async function insert(data) {
  const { db } = await getMongoConnect();
  const { user } = data;
  const date = format(new Date(), 'yyyy-MM-dd HH:mm:ss');

  let colect = {
    create_user: user,
    update_user: user,
    create_date: date,
    update_date: date
  };
  ['name', 'pwd', 'state', 'type', 'email'].forEach(key => {
    objectSetKey(colect, key, data[key]);
  });
  const result = await db.collection(collectionName).insertOne(colect);
  console.debug(`collection users has insert one. ${result.insertedId}.`);
  return result;
}

async function update(data) {
  const { db } = await getMongoConnect();
  const { _id, user } = data;
  const date = format(new Date(), 'yyyy-MM-dd HH:mm:ss');
  let colect = {
    update_date: date,
    update_user: user
  };
  ['name', 'pwd', 'state', 'type', 'email'].forEach(key => {
    objectSetKey(colect, key, data[key]);
  });
  const result = await db.collection(collectionName).updateOne(
    { _id: ObjectID(_id) },
    {
      $set: colect
    }
  );
  console.debug(`collection users updated ${result.modifiedCount}.`);
  return result;
}

async function deleteOne(data) {
  const { db } = await getMongoConnect();
  const { _id } = data;
  const result = await db
    .collection(collectionName)
    .deleteOne({ _id: { $eq: ObjectID(_id) } });
  console.debug(`collection users has deleted ${result.deletedCount}.`);
  return result;
}

async function login(data) {
  const { db } = await getMongoConnect();
  const { name, pwd } = data;
  const result = await db.collection(collectionName).findOne({
    $and: [{ name: { $eq: name } }, { pwd: { $eq: pwd } }]
  });
  return result;
}

export { insert, update, query, queryOne, deleteOne, login };
